//
//  VC6Emitter.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC6Emitter: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        // 设置发射器
        let fireEmitter = CAEmitterLayer()
        fireEmitter.emitterPosition = CGPoint(x: view.frame.width * 0.5, y: view.frame.height - 20)
        fireEmitter.emitterSize = CGSize(width: view.frame.width - 100, height: 20)
        fireEmitter.renderMode = .additive
        
        // 设置发射单元
        let fire = CAEmitterCell()
        fire.birthRate = 1600
        fire.lifetime = 4.0
        fire.lifetimeRange = 1.5
        fire.color = UIColor(displayP3Red: 0.8, green: 0.4, blue: 0.3, alpha: 0.1).cgColor
        fire.contents = UIImage(named: "ic_action_blob")?.cgImage
        fire.name = "fire"
        fire.velocity = 160
        fire.velocityRange = 80
        fire.emissionLatitude = CGFloat(Double.pi + Double.pi / 2)
        fire.emissionRange = CGFloat(Double.pi / 2)
        fire.scaleSpeed = 0.3
        fire.spin = 0.2
        
        // 设置烟雾
        let smoke = CAEmitterCell()
        smoke.birthRate = 800
        smoke.lifetime = 6.0
        smoke.lifetimeRange = 1.5
        smoke.color = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 0.05).cgColor
        smoke.contents = UIImage(named: "ic_action_blob")?.cgImage
        smoke.name = "smoke"
        smoke.velocity = 250
        smoke.velocityRange = 100
        smoke.emissionLatitude = CGFloat(Double.pi + Double.pi / 2)
        smoke.emissionRange = CGFloat(Double.pi / 2)
        
        fireEmitter.emitterCells = [fire,smoke]
        view.layer.addSublayer(fireEmitter)
    }
}
